(function($){
	
	$.util.namespace("mainpage.nav");
	$.util.namespace("mainpage.mainTabs");
	
	var mainTabs="#main_tabs";
	
	$(window).resize(function(){
		$("#main_tabs").tabs("resize");
	});
	
	mainpage.nav.initMenuClick = function() {
		var navList = $(".nav-list li a") ;
		navList.click(function(){
			var href = $(this).attr("data-href") ;
			var text = $(this).children("span").text() ;
			window.mainpage.addModuleTab({"title": text, "href": href});
		});
	};
	
	window.mainpage.addModuleTab = function(node){
		var n = node || {}, attrs = node.attributes || {}, opts = $.extend({}, n, { title: n.text }, attrs);
        if (!opts.href || opts.href === "#") { return; }
        window.mainpage.mainTabs.addModule(opts);
	};
	
	window.mainpage.mainTabs.jumeTab = function (which) { $(mainTabs).tabs("select", which); };
	
	window.mainpage.mainTabs.addModule = function(title, href, iconCls, iniframe, closable, refreshable, selected){
		var opts = window.mainpage.mainTabs.parseCreateTabArgs(arguments), isExists = window.mainpage.mainTabs.isExists(opts.title, opts.href);
		switch (isExists) {
	        case 0: window.mainpage.mainTabs.createTab(opts); break;
	        case 1: window.mainpage.mainTabs.createTab(opts); break;
	        case 2: window.mainpage.mainTabs.jumeTab(opts.title); break;
	        default: break;
		}
	};
	
	window.mainpage.mainTabs.tabDefaultOption = {
        title: "新建选项卡", href: "", closable: true, refreshable: true, iconCls: "", repeatable: true, selected: true
    };
	window.mainpage.mainTabs.parseCreateTabArgs = function(args) {
		var ret = {};
        if (!args || !args.length) {
            $.extend(ret, window.mainpage.mainTabs.tabDefaultOption);
        } else if (args.length == 1) {
            $.extend(ret, window.mainpage.mainTabs.tabDefaultOption, typeof args[0] == "object" ? args[0] : { href: args[0] });
        } else if (args.length == 2) {
            $.extend(ret, window.mainpage.mainTabs.tabDefaultOption, { title: args[0], href: args[1] });
        } else if (args.length == 3) {
            $.extend(ret, window.mainpage.mainTabs.tabDefaultOption, { title: args[0], href: args[1], iconCls: args[2] });
        } else if (args.length == 4) {
            $.extend(ret, window.mainpage.mainTabs.tabDefaultOption, { title: args[0], href: args[1], iconCls: args[2], iniframe: args[3] });
        } else if (args.length == 5) {
            $.extend(ret, window.mainpage.mainTabs.tabDefaultOption, { title: args[0], href: args[1], iconCls: args[2], iniframe: args[3], closable: args[4] });
        } else if (args.length == 6) {
            $.extend(ret, window.mainpage.mainTabs.tabDefaultOption, { title: args[0], href: args[1], iconCls: args[2], iniframe: args[3], closable: args[4], refreshable: args[5] });
        } else if (args.length >= 7) {
            $.extend(ret, window.mainpage.mainTabs.tabDefaultOption, { title: args[0], href: args[1], iconCls: args[2], iniframe: args[3], closable: args[4], refreshable: args[5], selected: args[6] });
        }
        return ret;
	} ;
	
	window.mainpage.mainTabs.createTab = function(title, href, iconCls, iniframe, closable, refreshable, selected) {
		var t = $(mainTabs), i = 0, opts = window.mainpage.mainTabs.parseCreateTabArgs(arguments);
        while (t.tabs("getTab", opts.title + (i ? String(i) : ""))) { i++; }
        t.tabs("add", opts);
		
	};
    
    window.mainpage.mainTabs.isExists = function(title, href) {
    	 var t = $(mainTabs), tabs = t.tabs("tabs"), array = $.array.map(tabs, function (val) { return val.panel("options"); }),
         list = $.array.filter(array, function (val) { return val.title == title; }), ret = list.length ? 1 : 0;
	     if (ret && $.array.some(list, function (val) { return val.href == href; })) { ret = 2; }
	     return ret;
    };
    
	
	
	
	
	
	
	
	
	
	
	
	
	
	window.mainpage.nav.initMenuClick() ;
	
	function p(str){
		console.info(str) ;
	}
})(jQuery);